package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.goods.GoodsSpecificationsValuesType;
import com.hoshiicloud.goods.param.category.GoodsSpecificationsValuesTypeAddParam;
import com.hoshiicloud.goods.param.category.GoodsSpecificationsValuesTypeQueryParam;
import com.hoshiicloud.goods.param.category.GoodsSpecificationsValuesTypeUpdateParam;
import com.hoshiicloud.goods.service.GoodsSpecificationsValuesTypeService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 规格值分类 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/goods-spec-values-type")
@Api(value = "规格值分类", tags = {"规格值分类"})
public class GoodsSpecificationsValuesTypeController {
    @Autowired
    private GoodsSpecificationsValuesTypeService goodsSpecificationsValuesTypeService;


    @ApiOperation(value = "分页查询规格值分类")
    @PostMapping("/getPage")
    public Response<Page<GoodsSpecificationsValuesType>> page(@RequestBody GoodsSpecificationsValuesTypeQueryParam param) {
        try {
            QueryWrapper<GoodsSpecificationsValuesType> queryWrapper = new QueryWrapper<GoodsSpecificationsValuesType>();
            queryWrapper.lambda()
                    .eq(GoodsSpecificationsValuesType::getCompanyId, param.getCompanyId()) //目前写死 登录做完后要从token 拿
                    .eq(StringUtils.isNotBlank(param.getName()), GoodsSpecificationsValuesType::getName, param.getName())
                    .eq(StringUtils.isNotBlank(param.getType()), GoodsSpecificationsValuesType::getType, param.getType())
                    .eq(GoodsSpecificationsValuesType::getDeleted, 0);
            Page<GoodsSpecificationsValuesType> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = "GoodsSpecificationsValuesTypeQueryParam{" + param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(goodsSpecificationsValuesTypeService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询规格值分类异常", e);
            return Response.failed("分页查询规格值分类异常");
        }
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询规格值类别")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(goodsSpecificationsValuesTypeService.getById(id));
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增规格值类别")
    public Response<Boolean> save(@RequestBody GoodsSpecificationsValuesTypeAddParam param) {
        // Bean数据转换
        GoodsSpecificationsValuesType entity;
        try {
            entity = BeanUtils.covertType(param, GoodsSpecificationsValuesType.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsSpecificationsValuesTypeService.save(entity));
    }


    @PutMapping("/update")
    @ApiOperation(value = "修改规格值分类")
    public Response<Boolean> update(@RequestBody GoodsSpecificationsValuesTypeUpdateParam param) {
        // Bean数据转换
        GoodsSpecificationsValuesType entity;
        try {
            entity = BeanUtils.covertType(param, GoodsSpecificationsValuesType.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsSpecificationsValuesTypeService.updateById(entity));
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除规格值分类")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        return Response.data(goodsSpecificationsValuesTypeService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     */
    @ApiOperation(value = "根据id集合查询规格值分类")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(goodsSpecificationsValuesTypeService.getByIds(ids, StringUtils.join(ids, "")));
    }

    @GetMapping("/dropdownBox")
    @ApiOperation(value = "获取下拉框数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "规格类型", dataType = "String", example = "尺码")})
    public Response<List<GoodsSpecificationsValuesType>> dropdownBox(@RequestParam("companyId") Long companyId, @RequestParam("type") String type) {
        String key = "DropdownBoxQuery{companyId=" + companyId + ",type=" + type + "}";
        return Response.data(goodsSpecificationsValuesTypeService.queryDropdownBox(companyId, type, key));
    }
}
